/*
 * Edit In Place
 * http://josephscott.org/code/js/eip/
 *
 * Version: 0.2.2
 * License: http://josephscott.org/code/js/eip/license.txt
 */

/* 
 * 20091008: Modified by sonofdelphi for Neptuner. 
 * Change summary follows:
 *
 * Removed innerHTML
 * Activation on double-click instead of single-click
 * Adds a new element instead of editing in place
 * 
 */

EditInPlace = function() { };
EditInPlace.settings = function(set) {
	var settings = {
		id:				false,
		save_url:		false,
		css_class:		'eip_editable',
		savebutton:		'eip_savebutton',
		cancelbutton:	'eip_cancelbutton',
		saving:			'eip_saving',
		empty:			'eip_empty',
		type:			'text',
		is_empty:		false,
		empty_text:		'Click To Add Comment',
		orig_text:		false,
		options:		{ },
		option_name:	false
	};

	for(var i in set) { settings[i] = set[i]; }

	return($(settings));
};

EditInPlace.formField = function(set) {
	var field = '';

	set['orig_text'] = 'Enter comment.';

	if(set['type'] == 'textarea') {
		var cols = 50;
		if(set['cols']) { cols = set['cols']; }
		var rows = (set['orig_text'].length / cols) + 3;
		if(set['rows']) { rows = set['rows']; }

		field = '<span id="' + set['id'] + '_editor"><textarea id="'
			+ set['id'] + '_edit" class="' + set['css_class'] + '" name="'
			+ set['id'] + '_edit" rows="' + rows + '" cols="'
			+ cols + '">' + set['orig_text'] + '</textarea><br />';
	}

	return(field);
};

EditInPlace.formButtons = function(set) {
	return(
		'<span><input id="' + set['id'] + '_save" class="'
		+ set['savebutton'] + '" type="button" value="ADD" /> OR '
		+ '<input id="' + set['id'] + '_cancel" class="' 
		+ set['cancelbutton'] + '" type="button" value="CANCEL" />'
		+ '</span></span>'
	);
};

EditInPlace.setEvents = function(set) {
	Event.observe(
		set['id'],
		'mouseover',
		function() { Element.addClassName(set['id'], set['css_class']); },
		false
	);
	Event.observe(
		set['id'],
		'mouseout',
		function() { Element.removeClassName(set['id'], set['css_class']); },
		false
	);
	Event.observe(
		set['id'],
		'dblclick',
		function() {

			var field = EditInPlace.formField(set);
			var button = EditInPlace.formButtons(set);

			new Insertion.After(set['id'], field + button);
			Field.focus(set['id'] + '_edit');

			Event.observe(
				set['id'] + '_save',
				'click',
				function() { EditInPlace.saveChanges(set); },
				false
			);
			Event.observe(
				set['id'] + '_cancel',
				'click',
				function() { EditInPlace.cancelChanges(set); },
				false
			);
		},
		false
	);
};

EditInPlace.saveComplete = function(t, set) {
	Element.removeClassName(set['id'], set['css_class']);
};

EditInPlace.saveFailed = function(t, set) {
	Element.removeClassName(set['id'], set['css_class']);
};

EditInPlace.saveChanges = function(set) {

	var new_text = unescape($F(set['id'] + '_edit'));
	
	var formatted = '';
	formatted += '<SPAN class="review" ';
	
	var temp = set['id'] + 'comment'; //TODO: for id-generation
	
	formatted += '>';
	formatted += new_text;
	formatted += '</SPAN>';
	formatted += '</BR>';
	
	Element.removeClassName(set['id'], set['css_class']);
	new Insertion.After(set['id'], formatted);

	Element.remove(set['id'] + '_editor');
	
	var targetFile = 'test.html';
	
	var params = 'id=' + set['id'] + '&content=' + new_text+'&filename='+ targetFile;

	var ajax_req = new Ajax.Request(
		set['save_url'],
		{
			method: 'post',
			postBody: params,
			onSuccess: function(t) { EditInPlace.saveComplete(t, set); },
			onFailure: function(t) { EditInPlace.saveFailed(t, set); }
		}
	);
	
};

EditInPlace.cancelChanges = function(set) {
	Element.remove(set['id'] + '_editor');
	Element.removeClassName(set['id'], set['css_class']);

	Element.show(set['id']);
}

EditInPlace.makeEditable = function(args) {
	var set = EditInPlace.settings(args);

	EditInPlace.setEvents(set);
}
